public class Solution {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
    }

    private void dfs(TreeNode root){
        if(root == null){
            return;
        }

        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        if(root.left != null){
            dfs(root.left);
        }

        if(root.right != null){
            dfs(root.right);
        }
    }

    public TreeNode invertTree(TreeNode root) {
        dfs(root);
        return root;
    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}
